package com.health.mapp.impl;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.health.commons.PropertiesService;
import com.health.mapp.bean.GetVideoPlayAuthReq;
import com.health.mapp.bean.GetVideoPlayAuthResp;
import com.health.mapp.bean.Header;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;

/**
 * Created by YYY-Work on 2017/6/2.
 */
public class GetVideoPlayAuthImpl implements ActionInterface {


    @Override
    public Response execute(Header header, Object reqBean) throws Exception {

        GetVideoPlayAuthReq req = (GetVideoPlayAuthReq) reqBean;
        GetVideoPlayAuthResp _resp=new GetVideoPlayAuthResp();

        DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", PropertiesService.getString("video.accesskey"), PropertiesService.getString("video.secret"));
        DefaultAcsClient client = new DefaultAcsClient(profile);
        GetVideoPlayAuthResponse response=getVideoPlayAuth(client,req.getVideoid());
        if (null!=response)
        {
            _resp.setPlayAuth(response);
            _resp.setErrorCode(ResultCode.OPERATION_SUCCESS);
            _resp.setErrorMsg(ResultCode.OPERATION_SUCCESS_NOTE);

        }else
        {
            _resp.setErrorCode(-1);
            _resp.setErrorMsg("获取视频信息失败");
        }
        return null;
    }

    GetVideoPlayAuthResponse getVideoPlayAuth(DefaultAcsClient client,String vid) {
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(vid);
        GetVideoPlayAuthResponse response = null;
        try {
            response = client.getAcsResponse(request);
        } catch (ServerException e) {
            throw new RuntimeException("GetVideoPlayAuthRequest Server failed");
        } catch (ClientException e) {
            throw new RuntimeException("GetVideoPlayAuthRequest Client failed");
        }
        return response;
    }

}
